iT邦幫忙

2024 iThome 鐵人賽

DAY 17
0

Dialog,在點擊按鈕等會跳出來的對話框,也可以去製作它的底圖。

我總和了我用過的10種Dialog,跟一個AlertDialog給大家參考,感謝網上大佬們的文章讓我學習到那麼多~(>_<。)\

分上下篇,不然都集一起太多了,不再說廢話我們開始吧!


開始前動作

  • .xml 我放了10個Button,我想以按鈕觸發Dialog
    (大概畫面,因為我是亂拉的,所以沒有很美觀,大家將就一下><)
    https://ithelp.ithome.com.tw/upload/images/20240925/20168454Wx41dZqCNH.png
    (完整程式碼)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.08" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.05" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.45" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.55" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.94" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.15" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.2" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.3" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.35" />

    <Button
        android:id="@+id/main_2btn_btn"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="2顆按鈕對話框"
        app:layout_constraintBottom_toTopOf="@+id/guideline6"
        app:layout_constraintEnd_toStartOf="@+id/guideline3"
        app:layout_constraintStart_toStartOf="@+id/guideline"
        app:layout_constraintTop_toTopOf="@+id/guideline2" />

    <Button
        android:id="@+id/main_3btn_btn"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginEnd="4dp"
        android:text="3顆按鈕對話框"
        app:layout_constraintBottom_toTopOf="@+id/guideline6"
        app:layout_constraintEnd_toStartOf="@+id/guideline5"
        app:layout_constraintStart_toStartOf="@+id/guideline4"
        app:layout_constraintTop_toTopOf="@+id/guideline2"
        app:layout_constraintVertical_bias="0.52" />

    <Button
        android:id="@+id/main_list_btn"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="列表對話框"
        app:layout_constraintBottom_toTopOf="@+id/guideline9"
        app:layout_constraintEnd_toStartOf="@+id/guideline3"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="@+id/guideline"
        app:layout_constraintTop_toTopOf="@+id/guideline7"
        app:layout_constraintVertical_bias="0.0" />

    <Button
        android:id="@+id/main_singleChoice_btn"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text=" 單選對話框"
        app:layout_constraintBottom_toTopOf="@+id/guideline9"
        app:layout_constraintEnd_toStartOf="@+id/guideline5"
        app:layout_constraintStart_toStartOf="@+id/guideline4"
        app:layout_constraintTop_toTopOf="@+id/guideline7" />

    <Button
        android:id="@+id/main_check_btn"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="複選對話框"
        app:layout_constraintBottom_toTopOf="@+id/guideline11"
        app:layout_constraintEnd_toStartOf="@+id/guideline3"
        app:layout_constraintStart_toStartOf="@+id/guideline"
        app:layout_constraintTop_toTopOf="@+id/guideline10" />

    <Button
        android:id="@+id/main_wait_btn"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="等待型對話框"
        app:layout_constraintBottom_toTopOf="@+id/guideline11"
        app:layout_constraintEnd_toStartOf="@+id/guideline5"
        app:layout_constraintStart_toStartOf="@+id/guideline4"
        app:layout_constraintTop_toTopOf="@+id/guideline10" />

    <Button
        android:id="@+id/main_progressbar_btn"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="進度條對話框"
        app:layout_constraintBottom_toTopOf="@+id/guideline13"
        app:layout_constraintEnd_toStartOf="@+id/guideline3"
        app:layout_constraintStart_toStartOf="@+id/guideline"
        app:layout_constraintTop_toTopOf="@+id/guideline12" />

    <Button
        android:id="@+id/main_date_btn"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text=" 日期選擇對話框"
        app:layout_constraintBottom_toTopOf="@+id/guideline13"
        app:layout_constraintEnd_toStartOf="@+id/guideline5"
        app:layout_constraintStart_toStartOf="@+id/guideline4"
        app:layout_constraintTop_toTopOf="@+id/guideline12" />

    <Button
        android:id="@+id/main_time_btn"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text=" 時間選擇對話框"
        app:layout_constraintBottom_toTopOf="@+id/guideline14"
        app:layout_constraintEnd_toStartOf="@+id/guideline3"
        app:layout_constraintStart_toStartOf="@+id/guideline"
        app:layout_constraintTop_toTopOf="@+id/guideline8" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.45" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline12"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.5" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline13"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.6" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline14"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.75" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.65" />

    <Button
        android:id="@+id/main_edit_dtn"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="輸入對話框"
        app:layout_constraintBottom_toTopOf="@+id/guideline14"
        app:layout_constraintEnd_toStartOf="@+id/guideline5"
        app:layout_constraintStart_toStartOf="@+id/guideline4"
        app:layout_constraintTop_toTopOf="@+id/guideline8" />
</androidx.constraintlayout.widget.ConstraintLayout>
  • MainActivity
    (宣告)
    https://ithelp.ithome.com.tw/upload/images/20240925/20168454ukOxE6wErX.png
    progressValue => 在後面模擬進度條需要

1.可輸入的對話框

//可輸入對話框(以按鈕觸發Dialog)
        editButton = findViewById(R.id.main_edit_dtn);
        editButton.setOnClickListener((v) -> {
            AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            final EditText editText = new EditText(this);
            //設置圖示
            dialog.setIcon(android.R.drawable.btn_star);
            //設置標題文字
            dialog.setTitle("標題文字");
            //使用輸入editText
            dialog.setView(editText);

            //設置左邊按鈕和點擊事件
            dialog.setNegativeButton("關閉", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    // TODO: ...
                }
            });
            //設置右邊按鈕和點擊事件
            dialog.setPositiveButton("確定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    // TODO: ...
                    Toast.makeText(context, editText.getText().toString() , Toast.LENGTH_SHORT).show();
                }
            });
            //顯示Dialog
            dialog.show();
        });

setNegativeButton => 設置左邊按鈕的事件
setPositiveButton => 設置右邊按鈕的事件
記得最後一定要打dialog.show();,才能顯示Dialog喔!

  • 執行畫面
    https://ithelp.ithome.com.tw/upload/images/20240925/20168454dLOPiZmMek.png
    (按下確認)
    https://ithelp.ithome.com.tw/upload/images/20240925/20168454ejPpyXVwgG.png

2.兩種按鈕的對話框

//兩種按鈕的對話框(以按鈕觸發Dialog)
        btn2Button = findViewById(R.id.main_2btn_btn); //綁定
        btn2Button.setOnClickListener((v) -> {
            AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            //設置圖示
            dialog.setIcon(android.R.drawable.btn_star);
            //設置標題文字
            dialog.setTitle("標題文字");
            //設置內文文字
            dialog.setMessage("內文文字");

            //設置左邊按鈕和點擊事件
            dialog.setNegativeButton("關閉", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    //可以設置Toast提醒等
                    // TODO: ...
                }
            });
            //設置右邊按鈕和點擊事件
            dialog.setPositiveButton("確定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    // TODO: ...
                }
            });
            //顯示Dialog
            dialog.show();
        });
  • 執行畫面
    https://ithelp.ithome.com.tw/upload/images/20240925/201684547wQO0pnUPJ.png

3.三種按鈕的對話框

//三種按鈕的對話框(以按鈕觸發Dialog)
        btn3Button = findViewById(R.id.main_3btn_btn);
        btn3Button.setOnClickListener((v) ->{
            AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            dialog.setTitle("標題文字");
            dialog.setMessage("內文文字");
            dialog.setIcon(android.R.drawable.btn_star);
            //左邊按鈕
            dialog.setNeutralButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    //TODO: ...
                }
            });
            //中間按鈕
            dialog.setNegativeButton("關閉", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    //TODO: ...
                }
            });
            //右邊按鈕
            dialog.setPositiveButton("確認", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    //TODO: ...
                }
            });
            dialog.show(); // 顯示dialog
        });
  • 執行畫面
    https://ithelp.ithome.com.tw/upload/images/20240925/201684543m9rRSVswn.png

4.列表對話框

//列表對話框
        listButton = findViewById(R.id.main_list_btn);
        listButton.setOnClickListener((v) -> {
            //設置列表字串文字
            String[] items = {"列表1", "列表2", "列表3", "列表4","列表5"};
            AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            dialog.setTitle("標題文字");
            // 設置item點擊事件處理
            dialog.setItems(items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Toast.makeText(context, "你選擇" + items[i], Toast.LENGTH_SHORT).show();
                }
            });
            dialog.show(); // 顯示dialog
        });

setItems => 設置列表字串文字項目

  • 執行畫面
    https://ithelp.ithome.com.tw/upload/images/20240925/20168454CjNAYz9T6m.png
    (按下選項)
    https://ithelp.ithome.com.tw/upload/images/20240925/20168454edPUhwj2HL.png

5.可選擇日期的對話框

//日期選擇對話框
        dateButton = findViewById(R.id.main_date_btn);
        dateButton.setOnClickListener((v) -> {
            Calendar calendar = Calendar.getInstance();
            DatePickerDialog dialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
                // 當日期選擇完畢並按下"OK"按鈕 的事件觸發處理
                @Override
                public void onDateSet(DatePicker datePicker, int y, int m, int d) {
                    StringBuilder result = new StringBuilder();
                    result.append(y).append("/").append(m+1).append("/").append(d);
                    Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
                }
            },calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
            dialog.show();
        });
  • 執行畫面
    https://ithelp.ithome.com.tw/upload/images/20240925/20168454HdmiVv4HCC.png
    (按下OK)
    https://ithelp.ithome.com.tw/upload/images/20240925/201684548WhqG0hG1n.png

6.可選擇時間的對話框

//時間選擇對話框
        timeButton = findViewById(R.id.main_time_btn);
        timeButton.setOnClickListener((v) -> {
            Calendar calendar = Calendar.getInstance();
            TimePickerDialog dialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
                // 當時間選擇完畢並按下"OK"按鈕 的事件觸發處理
                @Override
                public void onTimeSet(TimePicker timePicker, int h, int m) {
                    StringBuilder result = new StringBuilder();
                    result.append("hour:").append(h).append("-min:").append(m);
                    Toast.makeText(context, result, Toast.LENGTH_SHORT).show();
                }
            }, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true);
            dialog.show();
        });
  • 執行畫面
    https://ithelp.ithome.com.tw/upload/images/20240925/20168454vgV5OPq7TD.png
    (按下OK)
    https://ithelp.ithome.com.tw/upload/images/20240925/20168454WGrpx6eFyf.png

上半部先這樣,這些Dialog很實用,在做一些自己的APP,適當運用這些會有更多的豐富感(?

這些Dialog程式碼改成自己想要的內容就可以啦~用久了就會特別熟練。


上一篇
元件篇-Switch與CheckBox Day16
下一篇
元件篇-Dialog(下) Day18
系列文
Android 元件總動員 - 運用與實踐元件指南30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言